﻿using Microsoft.Data.Sqlite;
using SSC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace SSC.Services
{
    /// <summary>
    /// 提供料号类的增删改查
    /// </summary>
    public class PartService
    {
        public static List<PartModel> GetPartModels()
        {
            string query = @"
                    SELECT 
                        Id, 
                        PartName, 
                        BatchNumber, 
                        DateTime, 
                        Column1, 
                        Column2, 
                        Column3, 
                        Column4, 
                        Column5, 
                        Column6, 
                        Column7, 
                        Column8, 
                        Column9, 
                        Column10 
                    FROM tbProductRecipe";

            var partModels = App.DatabaseService.GetAll<PartModel>(query);
            return partModels;
        }

        public static bool AddPartModel(PartModel partModel)
        {
            string query = @"
                    INSERT INTO tbProductRecipe (
                        PartName, 
                        BatchNumber, 
                        DateTime, 
                        Column1, 
                        Column2, 
                        Column3, 
                        Column4, 
                        Column5, 
                        Column6, 
                        Column7, 
                        Column8, 
                        Column9, 
                        Column10
                    ) VALUES (
                        @PartName, 
                        @BatchNumber, 
                        @DateTime, 
                        @Column1, 
                        @Column2, 
                        @Column3, 
                        @Column4, 
                        @Column5, 
                        @Column6, 
                        @Column7, 
                        @Column8, 
                        @Column9, 
                        @Column10
                    )";

            return App.DatabaseService.Execute(query, partModel);
        }

        public static bool UpdatePartModel(PartModel partModel)
        {
            string query = @"
                    UPDATE tbProductRecipe SET 
                        PartName = @PartName, 
                        BatchNumber = @BatchNumber, 
                        DateTime = @DateTime, 
                        Column1 = @Column1, 
                        Column2 = @Column2, 
                        Column3 = @Column3, 
                        Column4 = @Column4, 
                        Column5 = @Column5, 
                        Column6 = @Column6, 
                        Column7 = @Column7, 
                        Column8 = @Column8, 
                        Column9 = @Column9, 
                        Column10 = @Column10
                    WHERE Id = @Id";

            return App.DatabaseService.Execute(query, partModel);
        }

        public static bool DeletePartModel(int id)
        {
            string query = "DELETE FROM tbProductRecipe WHERE Id = @Id";
            return App.DatabaseService.Execute(query, new { Id = id });
        }

        public static bool IsPartNameExists(string partName)
        {
            string query = "SELECT COUNT(1) FROM tbProductRecipe WHERE PartName = @PartName";
            var parameter = new SqliteParameter("@PartName", partName);
            int count = App.DatabaseService.ExecuteScalar<int>(query, parameter);
            return count > 0;
        }
    }
}
